﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using Maticsoft.Common;
using ChinaSoft.Model;
using Wuqi.Webdiyer;
using System.Web.UI.WebControls;

namespace ChinaSoft.BLL.Common
{
    public class Pager
    {
        private readonly DAL.Common.Pager pager = new DAL.Common.Pager();
        /// <summary>
        /// 根据要查询的sql语句查询出总共有多少条记录
        /// 作者：卢亚东
        /// 时间：2011-07-11
        /// </summary>
        /// <param name="strWhere">SQL查询语句</param>
        /// <returns></returns>
        public int RecordCount(string tableName, string strWhere)
        {
           return pager.RecordCount(tableName, strWhere);
        }
        /// <summary>
        /// 有排序，分组功能的分页获取数据
        /// 作者：卢亚东
        /// 修改时间：2011-7-14
        /// </summary>
        /// <param name="tableName">表名（必填）</param>
        /// <param name="strKey">主键名(可空，默认是ID)</param>
        /// <param name="PageSize">每页的个数（可空，默认是10个）</param>
        /// <param name="PageIndex">第几页（必填)</param>
        /// <param name="strFields">要查询的字段（可空，默认是*）</param>
        /// <param name="strWhere">查询条件（可空，没有where，以and开头）</param>
        /// <param name="strOrder">排序条件(可空，没有order by)</param>
        /// <param name="strGroupBy">分组条件(可空，没有group by)</param>
        /// <param name="recordCount">总页数</param>
        /// <returns></returns>
        public DataSet GetList(string tableName, string strKey, int PageSize, int PageIndex, string strFields, string strWhere, string strOrder, string strGroupBy, ref int recordCount)
        {
            recordCount = pager.RecordCount(tableName, strWhere);
            return pager.GetList(tableName, strKey, PageSize, PageIndex, strFields, strWhere, strOrder, strGroupBy);
        }
        /// <summary>
        /// 有排序功能的分页获取数据
        /// 作者：卢亚东
        /// 时间：2011-7-14
        /// </summary>
        /// <param name="tableName">表名(必填)</param>
        /// <param name="strKey">主键名(可空，默认是ID)</param>
        /// <param name="PageSize">每页的个数（可空，默认是10个）</param>
        /// <param name="PageIndex">第几页(必填)</param>
        /// <param name="strFields">要查询的字段（可空，默认是*）</param>
        /// <param name="strWhere">查询条件（可空，没有where，以and开头）</param>
        /// <param name="strOrder">排序条件(可空，没有order by)</param>
        /// <param name="recordCount">总页数</param>
        /// <returns></returns>
        public DataSet GetList(string tableName, string strKey, int PageSize, int PageIndex, string strFields, string strWhere, string strOrder, ref int recordCount)
        {
            recordCount = pager.RecordCount(tableName, strWhere);
            return pager.GetList(tableName, strKey, PageSize, PageIndex, strFields, strWhere, strOrder, null);
        }
        /// <summary>
        /// 绑定GridView和分页控件(要求分页控件的每页个数以设置)
        /// 作者：卢亚东
        /// 时间：2011-7-14
        /// </summary>
        /// <param name="gridview">GridView控件名(必填)</param>
        /// <param name="pager">分页控件名(必填)</param>
        /// <param name="tableName">表名(必填)</param>
        /// <param name="strKey">主键名(可空，默认为ID)</param>
        /// <param name="strFields">要查询的字段（可空，默认为*）</param>
        /// <param name="strWhere">查询条件(可空，不带where，以and开头)</param>
        /// <param name="strOrder">排序条件（可空，不带order by）</param>
        /// <param name="strGroupBy">分组条件(可空，不带group by)</param>
        public static void BindGridView(GridView gridview, AspNetPager pager, string tableName, string strKey, string strFields, string strWhere, string strOrder, string strGroupBy)
        {
            Pager pagerclass = new Pager();
            int recordCount = 0;
            DataSet ds = pagerclass.GetList(tableName, strKey, pager.PageSize, pager.CurrentPageIndex, strFields, strWhere, strOrder, strGroupBy, ref recordCount);
            pager.RecordCount = recordCount;
            gridview.DataSource = ds;
            gridview.DataBind();
        }
        /// <summary>
        /// 绑定Reater控件和分页控件(要求分页控件的每页个数以设置)
        /// 作者：卢亚东
        /// 时间：2011-7-14
        /// </summary>
        /// <param name="repeater">Repeater控件名称(必填)</param>
        /// <param name="pager">分页控件名称（必填）</param>
        /// <param name="tableName">表名（必填）</param>
        /// <param name="strKey">主键名(可空，默认为ID)</param>
        /// <param name="strFields">要查询的字段（可空，默认为*）</param>
        /// <param name="strWhere">查询条件（可空，不带where，以and开头的）</param>
        /// <param name="strOrder">排序条件（可空，不带order by）</param>
        /// <param name="strGroupBy">分组条件（可空，不带group by）</param>
        public static void BindRepeater(Repeater repeater, AspNetPager pager, string tableName, string strKey, string strFields, string strWhere, string strOrder, string strGroupBy)
        {
            Pager pagerclass = new Pager();
            int recordCount = 0;
            DataSet ds = pagerclass.GetList(tableName, strKey, pager.PageSize, pager.CurrentPageIndex, strFields, strWhere, strOrder, strGroupBy, ref recordCount);
            pager.RecordCount = recordCount;
            repeater.DataSource = ds;
            repeater.DataBind();
        }
        /// <summary>
        /// 绑定Reater控件和分页控件(要求分页控件的每页个数以设置)
        /// 作者：卢亚东
        /// 时间：2011-7-19
        /// </summary>
        /// <param name="repeater">DataList控件名称(必填)</param>
        /// <param name="pager">分页控件名称（必填）</param>
        /// <param name="tableName">表名（必填）</param>
        /// <param name="strKey">主键名(可空，默认为ID)</param>
        /// <param name="strFields">要查询的字段（可空，默认为*）</param>
        /// <param name="strWhere">查询条件（可空，不带where，以and开头的）</param>
        /// <param name="strOrder">排序条件（可空，不带order by）</param>
        /// <param name="strGroupBy">分组条件（可空，不带group by）</param>
        public static void BindDataList(DataList datalist, AspNetPager pager, string tableName, string strKey, string strFields, string strWhere, string strOrder, string strGroupBy)
        {
            Pager pagerclass = new Pager();
            int recordCount = 0;
            DataSet ds = pagerclass.GetList(tableName, strKey, pager.PageSize, pager.CurrentPageIndex, strFields, strWhere, strOrder, ref recordCount);
            pager.RecordCount = recordCount;
            datalist.DataSource = ds;
            datalist.DataBind();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <returns></returns>
        public static DataTable TablePage(DataTable dt, int PageSize, int PageIndex)
        {
            int count = dt.Rows.Count;
            int PageMin = PageSize * (PageIndex - 1);
            int PageMax = PageSize * (PageIndex - 1) + PageSize;
            for (int i = 0; i < count; i++)
            {
                if (i < PageMin || i >= PageMax)
                {
                    dt.Rows.RemoveAt(i);
                    count--;
                    i--;
                    if (i < PageMin)
                    {
                        PageMin--;
                        PageMax--;
                    }
                }
            }
            return dt;
        }

        /// <summary>
        /// 获取分页查询条件
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="condition">查询条件 (注意: 不要加 where)</param>
        /// <returns></returns>
        public string[] GetSearchCondition(string tableName, string condition)
        {
            string columns = "*";
            string sort = "order by ID";//"order by roleId";
            string idColumnName = "ID";
            string[] tmpParameters = { columns, tableName, condition, sort, idColumnName };
            return tmpParameters;
        }

    }
}
